<!-- 轮播图管理 -->
<template>
  <div class="main-wrapper">
    <div class="filter-container">
      <el-select v-model="listQuery.type" placeholder="类型" clearable class="filter-item">
        <el-option label="项目信息" :value="1" />
        <el-option label="咨询信息" :value="2" />
        <el-option label="用户信息" :value="3" />
        <el-option label="广告" :value="4" />
      </el-select>
      <el-select v-model="listQuery.status" placeholder="状态" clearable class="filter-item">
        <el-option v-for="item in statusOptions" :key="item.key" :label="item.statusName" :value="item.key" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row>
      <el-table-column label="轮播图片" align="center">
        <template slot-scope="{row}">
          <el-image v-if="row.imgUrl" :src="getFullImageUrl(row.imgUrl)" />
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center">
        <template slot-scope="{row}">
          {{ row.type | typeFilter() }}
        </template>
      </el-table-column>
      <el-table-column label="展示顺序" align="center">
        <template slot-scope="{row}">
          {{ row.sort }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.status | styleFilter()">{{ row.status | statusFilter() }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">修改</el-button>
          <el-button v-if="row.status!=0" size="mini" type="danger" @click="delCarousel(row)">删除</el-button>
          <el-button v-if="row.status!=1" size="mini" type="success" @click="reuseCarousel(row)">恢复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="carouselData" label-position="left" label-width="110px">
        <el-form-item label="轮播图片">
          <el-upload
            class="image-uploader"
            action="#"
            :show-file-list="false"
            :http-request="uploadImage"
            :on-change="uploadOnChange"
          >
            <img v-if="carouselData.imgUrl" :src="fullImageUrl" class="avatar">
            <i v-else class="el-icon-upload avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="carouselData.type" placeholder="请选择">
            <el-option v-for="item in carouselTypeList" :key="item.id" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="展示内容选择">
          <el-button type="text" @click="showObjectList">选择展示内容</el-button> <span>{{ carouselData.objectName }}</span>
        </el-form-item>
        <el-form-item label="展示顺序">
          <el-input v-model="carouselData.sort" type="number" style="width:200px;" placeholder="请填写展示顺序" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="carouselData.status" class="filter-item" placeholder="请选择">
            <el-option v-for="item in statusOptions" :key="item.key" :label="item.statusName" :value="item.key" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createCarousel():updateCarousel()">提交</el-button>
      </div>
    </el-dialog>

    <!-- 资讯信息 -->
    <el-dialog title="选择链接对象" :visible.sync="dialogInfoVisible">
      <el-table
        ref="multipleTable"
        :data="infoList"
        tooltip-effect="dark"
        style="width: 100%"
        highlight-current-row
        @current-change="handleSelectionChange"
      >
        <el-table-column label="资讯信息标题" align="center">
          <template slot-scope="{row}">
            {{ row.title }}
          </template>
        </el-table-column>
        <el-table-column label="作者" align="center">
          <template slot-scope="{row}">
            {{ row.fromName }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="{row}">
            <span>{{ row.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="状态" align="center" width="92">
          <template slot-scope="{row}">
            <el-tag :type="row.status | styleFilter()">{{ row.statusName }}</el-tag>
          </template>
        </el-table-column> -->
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogInfoVisible = false">取 消</el-button>
        <el-button type="primary" @click="createObject">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 项目信息 -->
    <el-dialog title="选择项目" top="3vh" width="68%" :visible.sync="dialogProjectVisible">
      <el-table
        ref="multipleTable"
        :data="projectList"
        tooltip-effect="dark"
        style="width: 100%"
        highlight-current-row
        @current-change="handleSelectionChange"
      >
        <el-table-column label="项目名称" align="center">
          <template slot-scope="{row}">
            {{ row.name }}
          </template>
        </el-table-column>
        <el-table-column label="所在省市" align="center">
          <template slot-scope="{row}">
            {{ row.provinceName }}{{ row.cityName }}
          </template>
        </el-table-column>
        <el-table-column label="项目价格" align="center">
          <template slot-scope="{row}">
            {{ (row.price/100) }}
          </template>
        </el-table-column>
        <el-table-column label="项目类型" align="center">
          <template slot-scope="{row}">
            {{ row.categoryName }}
          </template>
        </el-table-column>
        <el-table-column label="专业" align="center">
          <template slot-scope="{row}">
            {{ row.professionName }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="{row}">
            <span>{{ row.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="92">
          <template slot-scope="{row}">
            <el-tag :type="row.status | styleFilter()">{{ row.statusName }}</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogProjectVisible = false">取 消</el-button>
        <el-button type="primary" @click="createObject">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 用户信息 -->
    <el-dialog title="选择用户" top="3vh" width="68%" :visible.sync="dialogUserVisible">
      <el-table
        ref="multipleTable"
        :data="userList"
        tooltip-effect="dark"
        style="width: 100%"
        highlight-current-row
        @current-change="handleSelectionChange"
      >
        <el-table-column label="用户名称" align="center">
          <template slot-scope="{row}">
            {{ row.name }}
          </template>
        </el-table-column>
        <el-table-column label="项目类型" align="center">
          <template slot-scope="{row}">
            {{ row.busiScope }}
          </template>
        </el-table-column>
        <el-table-column label="专业" align="center">
          <template slot-scope="{row}">
            {{ row.profScope }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUserVisible = false">取 消</el-button>
        <el-button type="primary" @click="createObject">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 案例信息 -->
    <el-dialog title="选择案例" top="3vh" width="68%" :visible.sync="dialogCaseVisible">
      <el-table
        ref="multipleTable"
        :data="projectCaseList"
        tooltip-effect="dark"
        style="width: 100%"
        highlight-current-row
        @current-change="handleSelectionChange"
      >
        <el-table-column label="案例标题" align="center">
          <template slot-scope="{row}">
            {{ row.title }}
          </template>
        </el-table-column>
        <el-table-column label="作者" align="center">
          <template slot-scope="{row}">
            {{ row.author }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCaseVisible = false">取 消</el-button>
        <el-button type="primary" @click="createObject">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCarouselList, addCarousel, editCarousel, editCarouselStatus } from '@/api/carousel'
import { getByXml, getImageUrl, parseTime } from '@/utils'
import { uploadFileData } from '@/api/upload'
import { getProjectList } from '@/api/project'
import { getIndustryInfoList } from '@/api/industryInfo'
import { getUserList } from '@/api/user'
import { getProjectCaseList } from '@/api/projectCase'

export default {
  name: 'Carousel',
  components: {
  },
  filters: {
    styleFilter(style) {
      const styleMap = { 1: 'success', 0: 'danger' }
      return styleMap[style]
    },
    timeFilter(time, format) {
      return parseTime(time, format)
    },
    statusFilter(value) {
      const statusMap = { 0: '无效', 1: '有效' }
      return statusMap[value]
    },
    typeFilter(value) {
      const typeMap = { 1: '项目信息', 2: '资讯信息', 3: '用户信息', 4: '经典案例' }
      return typeMap[value]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      statusOptions: [
        { key: 0, statusName: '无效' },
        { key: 1, statusName: '有效' }
      ],
      carouselTypeList: [
        { id: 1, value: '项目信息', url: '/project-info/' },
        { id: 2, value: '资讯信息', url: '/info/detail/' },
        { id: 3, value: '用户信息', url: '/user-info/' },
        { id: 4, value: '经典案例', url: '/case/detail/' }
      ],
      carouselData: {
        imgUrl: '', sort: 1, status: 1, objectName: ''
      },
      dialogFormVisible: false,
      dialogDetailVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      projectList: [],
      dialogProjectVisible: false,
      infoList: [],
      dialogInfoVisible: false,
      projectCaseList: [],
      dialogCaseVisible: false,
      userList: [],
      dialogUserVisible: false,
      rules: {}
    }
  },
  computed: {
    fullImageUrl() {
      return this.getFullImageUrl(this.carouselData.imgUrl)
    }
  },
  created() {
    this.fetchCarouselList()
  },
  methods: {
    fetchCarouselList() {
      this.listLoading = true
      getCarouselList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchCarouselList()
    },
    resetCarouselData() {
      this.carouselData = { imgUrl: '', sort: 1, status: 1 }
    },
    handleCreate() {
      this.resetCarouselData()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createCarousel() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.carouselData)
        if (valid) {
          addCarousel(this.carouselData).then((response) => {
            this.fetchCarouselList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.carouselData = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateCarousel() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.carouselData)
          editCarousel(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.carouselData.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.carouselData)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    delCarousel(row) {
      row.status = 0
      this.updateCarouselStatus(row, 0)
      this.$notify({
        title: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
    },
    reuseCarousel(row) {
      row.status = 1
      this.updateCarouselStatus(row, 1)
      this.$notify({
        title: '成功',
        message: '数据恢复成功',
        type: 'success',
        duration: 2000
      })
    },
    updateCarouselStatus(row, status) {
      row.status = status
      editCarouselStatus(row.id, status).then(() => {
        this.carouselData = Object.assign({}, row)
        this.carouselData.status = status
        for (const v of this.list) {
          if (v.id === this.carouselData.id) {
            const index = this.list.indexOf(v)
            this.list.splice(index, 1, this.carouselData)
            break
          }
        }
      })
    },
    uploadImage(e) {
      uploadFileData(e.file).then(res => {
        this.carouselData.imgUrl = getByXml(res, 'Key')
      })
    },
    getFullImageUrl(imgName) {
      return getImageUrl(imgName)
    },
    uploadOnChange(file, fileList) {
    },
    showObjectList() {
      const type = this.carouselData.type
      if (type === 1) {
        getProjectList({}).then(res => {
          this.projectList = res.data.items
          console.log(res.data)
          this.dialogProjectVisible = true
        })
      } else if (type === 2) {
        getIndustryInfoList({}).then(res => {
          this.infoList = res.data.items
          console.log(res.data)
          this.dialogInfoVisible = true
        })
      } else if (type === 3) {
        getUserList({}).then(res => {
          this.userList = res.data.items
          console.log(res.data)
          this.dialogUserVisible = true
        })
      } else {
        getProjectCaseList({}).then(res => {
          this.projectCaseList = res.data.items
          console.log(res.data)
          this.dialogCaseVisible = true
        })
      }
    },
    createObject() {
      if (!this.carouselData.objectId) {
        this.$message({
          message: '请选择一条信息',
          type: 'warning'
        })
      }
      this.dialogProjectVisible = false
      this.dialogInfoVisible = false
      this.dialogUserVisible = false
      this.dialogCaseVisible = false
    },
    handleSelectionChange(val) {
      this.carouselData.objectId = val.id
      if (this.carouselData.type === 1 || this.carouselData.type === 3) {
        this.carouselData.objectName = val.name
      } else {
        this.carouselData.objectName = val.title
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.main-wrapper {
  margin: 8px;
  .filter-container {
    padding: 10px 0;
    .filter-item {
      width: 200px;
      margin-right: 10px;
    }
  }
  .image-uploader {
    border: 1px dashed #d9d9d9;
    width: 278px;
    height: 178px;
    .el-upload {
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 278px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 278px;
      height: 178px;
      display: block;
    }
  }
}
</style>
